package com.nd.service;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.BrandMapper;
import com.nd.pojo.bean.Brand;
import com.nd.pojo.bean.BrandExample;
import com.nd.pojo.bo.BrandCreateBO;
import com.nd.pojo.bo.BrandUpdateBO;
import com.nd.pojo.vo.BrandCreateVO;
import com.nd.pojo.vo.BrandListVO;
import com.nd.pojo.vo.BrandUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-04 23:29
 **/
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandListVO brangList(Integer page, Integer limit, Integer id, String name, String sort, String order) {

        if (id != null && !"".equals(id) && name != null && !"".equals(name)) {
            PageHelper.startPage(page, limit);
            BrandExample brandExample = new BrandExample();
            brandExample.setOrderByClause(sort + " " + order);
            BrandExample.Criteria criteria = brandExample.createCriteria();
            criteria.andIdEqualTo(id).andNameLike("%" + name + "%").andDeletedEqualTo(false);
            List<Brand> brandList = brandMapper.selectByExample(brandExample);
            PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);
            long total = brandPageInfo.getTotal();
            BrandListVO brandListVO = new BrandListVO();
            brandListVO.setTotal(total);
            brandListVO.setItems(brandList);
            return brandListVO;
        } else if (id != null && !"".equals(id)){
            PageHelper.startPage(page, limit);
            BrandExample brandExample = new BrandExample();
            brandExample.setOrderByClause(sort + " " + order);
            BrandExample.Criteria criteria = brandExample.createCriteria();
            criteria.andIdEqualTo(id).andDeletedEqualTo(false);

            List<Brand> brandList = brandMapper.selectByExample(brandExample);
            PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);
            long total = brandPageInfo.getTotal();
            BrandListVO brandListVO = new BrandListVO();
            brandListVO.setTotal(total);
            brandListVO.setItems(brandList);
            return brandListVO;
        } else if (name != null && !"".equals(name)) {
            PageHelper.startPage(page, limit);
            BrandExample brandExample = new BrandExample();
            brandExample.setOrderByClause(sort + " " + order);
            BrandExample.Criteria criteria = brandExample.createCriteria();
            criteria.andNameLike("%" + name + "%").andDeletedEqualTo(false);
            List<Brand> brandList = brandMapper.selectByExample(brandExample);
            PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);
            long total = brandPageInfo.getTotal();
            BrandListVO brandListVO = new BrandListVO();
            brandListVO.setTotal(total);
            brandListVO.setItems(brandList);
            return brandListVO;
        } else {
            PageHelper.startPage(page, limit);
            BrandExample brandExample = new BrandExample();
            brandExample.setOrderByClause(sort + " " + order);
            BrandExample.Criteria criteria = brandExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<Brand> brandList = brandMapper.selectByExample(brandExample);
            PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);
            long total = brandPageInfo.getTotal();
            BrandListVO brandListVO = new BrandListVO();
            brandListVO.setTotal(total);
            brandListVO.setItems(brandList);
            return brandListVO;
        }
    }

    @Override
    public BrandCreateVO brandCreate(BrandCreateBO brandCreateBO) {

        BrandCreateVO brandCreateVO = new BrandCreateVO();

        Brand brand = new Brand();

        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        byte max = brandMapper.findMaxSortOrder();

        Date time = DateTime.now();

        brand.setName(brandCreateBO.getName());
        brand.setDesc(brandCreateBO.getDesc());
        brand.setPicUrl(brandCreateBO.getPicUrl());
        brand.setSortOrder(++max);
        brand.setFloorPrice(new BigDecimal(brandCreateBO.getFloorPrice()));
        brand.setAddTime(time);
        brand.setUpdateTime(time);
        brand.setDeleted(false);

        brandMapper.brandCreate(brand);

        BrandExample brandExample2 = new BrandExample();
        BrandExample.Criteria criteria2 = brandExample2.createCriteria();
        criteria2.andSortOrderEqualTo(max);
        List<Brand> brands = brandMapper.selectByExample(brandExample2);
        int id = 0;
        for (Brand brand1 : brands) {
            id = brand1.getId();
        }
        brandCreateVO.setId(id);
        brandCreateVO.setName(brandCreateBO.getName());
        brandCreateVO.setDesc(brandCreateBO.getDesc());
        brandCreateVO.setPicUrl(brandCreateBO.getPicUrl());
        brandCreateVO.setFloorPrice(new BigDecimal(brandCreateBO.getFloorPrice()));
        brandCreateVO.setAddTime(time);
        brandCreateVO.setUpdateTime(time);

        return brandCreateVO;
    }

    @Override
    public BrandUpdateVO brandUpdate(BrandUpdateBO brandUpdateBO) {
        BrandUpdateVO brandUpdateVO = new BrandUpdateVO();

        Brand brand = new Brand();

        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();

        Date time = DateTime.now();
        brand.setId(brandUpdateBO.getId());
        brand.setName(brandUpdateBO.getName());
        brand.setDesc(brandUpdateBO.getDesc());
        brand.setPicUrl(brandUpdateBO.getPicUrl());
        brand.setFloorPrice(new BigDecimal(brandUpdateBO.getFloorPrice()));
        brand.setUpdateTime(time);
        brandMapper.updateByPrimaryKeySelective(brand);

        BrandExample brandExample2 = new BrandExample();
        BrandExample.Criteria criteria2 = brandExample2.createCriteria();
        criteria2.andIdEqualTo(brandUpdateBO.getId());
        List<Brand> brands = brandMapper.selectByExample(brandExample2);
        for (Brand brand1 : brands) {
            brandUpdateVO.setId(brand1.getId());
            brandUpdateVO.setName(brand1.getName());
            brandUpdateVO.setDesc(brand1.getDesc());
            brandUpdateVO.setPicUrl(brand1.getPicUrl());
            brandUpdateVO.setSortOrder(brand1.getSortOrder());
            brandUpdateVO.setFloorPrice(brand1.getFloorPrice());
            brandUpdateVO.setAddTime(brand1.getAddTime());
            brandUpdateVO.setUpdateTime(time);
            brandUpdateVO.setDeleted(false);
        }

        return brandUpdateVO;
    }

    @Override
    public void brandDelete(Integer id) {
        int a = brandMapper.deleteById(id);

    }
}
